<?php
$inc_lib = realpath(dirname(__FILE__) . '/../../teia.lib/inc/inc_core.inc');
require_once $inc_lib;
use teia\webdev\php\core as teia;

$inc_cust = realpath(dirname(__FILE__) . '/../inc/inc_cust.inc');
require_once $inc_cust;
use teia\webdev\php\cust as cust;

$result = teia\SessionWrapper::start();
if (!$result) {
	echo "Session Error: Failed to start session\n";
	exit(-1);
}
if (!isset($started)) {
	$started = true;
}

$cart = teia\SessionWrapper::get_cart(null);
$prod_it_track = teia\SessionWrapper::get_prod_it_track();

$prod_id = isset($_REQUEST['prod_id']) ? $_REQUEST['prod_id'] : '';
$item_id = isset($_REQUEST['item_id']) ? $_REQUEST['item_id'] : '';
$cart_qty = isset($_REQUEST['cart_qty']) ? $_REQUEST['cart_qty'] : 1;

if (isset($_REQUEST['form_name']) && $_REQUEST['form_name'] == 'add_cart_form') {
	$item_link = $_REQUEST['item_link'];
	$cart->add_cart_item($prod_id, $item_id, $cart_qty, $item_link);	
	$prod_it_track->push_prod_it($prod_id, $item_id, 0, 1);
} elseif (isset($_REQUEST['action'])) {
	if ($_REQUEST['action'] == 'rm') {
		$cart->remove_cart_item($prod_id, $item_id);
	} elseif ($_REQUEST['action'] == 'update_qty') {
		$cart->update_cart_item($prod_id, $item_id, $cart_qty);
	}

}
?>

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Shoes Boutique: Carrinho de Compra</title>
<link href="/style/layout.css" rel="stylesheet" type="text/css">
<link href="/style/shoppingcart.css" rel="stylesheet" type="text/css">
		<?php teia\FunctionPool::include_jquery(); ?>
		<style type="text/css">
			.auto-style1 {
				margin-left: 0;
			}
		</style>
	</head>
<body>
	<div align="center"
		style="position: absolute; margin-left: inherit; margin-right: inherit">
		<!-- Display header logo -->
			<?php cust\CustFunctions::display_header_logo(); ?>
			<!-- Display top navigation -->
			<?php cust\CustFunctions::display_top_nav(array('show_search' => false)); ?>
			<!-- Display ad-banner-->
			<?php cust\CustFunctions::display_top_banner(); ?>
			<?php //cust\CustFunctions::display_menu(); ?>
			<div id="container">  
    			<?php  $cart->display('page'); ?>
    			<div class="similar_items" id="recom_items">
    				<br />
    				<h2>Recomendações</h2>
    				<hr />
				<?php //echo $prod_obj->generate_all_thumbnails(false);
			$fields = array('prod_filter' => array('prod_type', 'prod_gender'), 'item_filter' => array('item_size'));
			$recom_filter = $cart->get_filter_values($fields);
			//print_r($recom_filter);
			//echo "<br />";
			$recom_product_listing = new teia\ProductCatalog('_product_catalog_', 'ProductCatalog', null, $recom_filter, 8);
			$recom_paginator = new teia\PaginationProvider('_pagination_', 'Pagination', null, $recom_product_listing);
			$recom_paginator->show_nav(false);
			@$recom_paginator->display('page');
			?>
				</div>
				<br />
				<div class="similar_items" id="history_items">
    				<h2>Histórico</h2>
    				<hr />
				<?php //echo $prod_obj->generate_all_thumbnails(false);
			$hist_filter = $prod_it_track->generate_filter(1, 0, 8);
			$empty = teia\FunctionPool::array_empty($hist_filter);
			if (!$empty) {
				$hist_product_listing = new teia\ProductCatalog('_product_catalog_', 'ProductCatalog', null, $hist_filter);
				$hist_paginator = new teia\PaginationProvider('_pagination_', 'Pagination', null, $hist_product_listing);
				$hist_paginator->show_nav(false);
				@$hist_paginator->display('page');
			} else {
				echo "<p>Ainda não há histórico</p>";
			}
			
			?>
				</div>
			</div>
			<div id="right_nav_bar">
				<p><span>Nº de itens no carrinho:</span><?php echo $cart->get_size();?></p>
				<p class="subtotal"><span>Subtotal:</span>$<?php echo $cart->get_subtotal();?></p>
				<p><a href="/compra/purchase.php">Checkout</a></p>
			</div>
			<?php //cust\CustFunctions::display_footer(); ?>
		</div>
</body>
</html>
